﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yyyg.Models;
using Yyyg.IDAL;
using Newtonsoft.Json;
using System.Data.Entity;
using static Yyyg.DAL.DbContextFactory;

namespace Yyyg.DAL
{
    public class GoodsDAL : BaseDAL<Goods>, IGoodsDAL
    {
        DbSet<Goods> table = DbLucky.Set<Goods>();

        /// <summary>
        /// 更新商品
        /// </summary>
        public void Update(int gid, string title, decimal price, int tyid, int stock, string brin, string main_url, string[] deturls, string[] subimgs, bool enb)
        {
            var g = table.Find(gid);
            if (g != null)
            {
                g = new Goods()
                {
                    Title = title,
                    Price = price,
                    Typeid = tyid,
                    Stock = stock,
                    Brin = brin,
                    Mainimg_url = main_url,
                    Detimgs_url = JsonConvert.SerializeObject(deturls),
                    Subimgs_url = JsonConvert.SerializeObject(subimgs),
                    Enable = enb
                };
                Update(g);
            }
            SaveChanges();
        }

        /// <summary>
        /// 上/下架指定商品
        /// </summary>
        public void OnOffShelves(int gid, bool isOn = true)
        {
            var g = table.Find(gid);
            if (g == null) return;
            if (g.Enable == isOn) return;
            g.Enable = true;
            SaveChanges();
        }

        /// <summary>
        /// 根据Guid查询商品
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public Goods GetGoodByGuid(string guid)
        {
            if (guid.Length == 0) return null;
            return table.FirstOrDefault(g => g.Guid == guid);
        }

        /// <summary>
        /// 根据参数查询商品信息
        /// </summary>
        /// <param name="qpar"></param>
        /// <returns></returns>
        public List<Goods> GetGoods(string qpar)
        {
            if (string.IsNullOrEmpty(qpar)) return GetAll().ToList();
            return table.Where(g => g.Gid.ToString() == qpar || g.Title.Contains(qpar)).ToList();
        }

        /// <summary>
        /// 根据分页信息异步查询商品,页码从1开始
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public async Task<List<Goods>> GetGoodsPageAsync(int pagesize, int index)
        {
            if (pagesize <= 0 || index <= 0) throw new ApplicationException("页长或页码小于等于0");
            return await table.Skip((index - 1) * pagesize).Take(pagesize).ToListAsync();
        }

        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <returns></returns>
        public int GetSumPages(int pagesize)
        {
            var s = table.Count();
            var sum = s / pagesize;
            if (s % pagesize > 0) sum++;
            return sum;
        }

        /// <summary>
        /// 返回商品分类一二级列表
        /// </summary>
        /// <returns></returns>
        public Pair<string, string>[][] GetTypes()
        {
            var fs = DbLucky.Goods_type.Where(t => t.Parentid < 0).Select(t => new Pair<string, string>
            {
                Value1 = t.Typename,
                Value2 = "{\"typeid\":\"" + t.Typeid + "\",\"parentid\":\"" + t.Parentid + "\"}"
            }).ToArray();
            var ss = DbLucky.Goods_type.Where(t => t.Parentid > 0).Select(t => new Pair<string, string>
            {
                Value1 = t.Typename,
                Value2 = "{\"typeid\":\"" + t.Typeid + "\",\"parentid\":\"" + t.Parentid + "\"}"
            }).ToArray();
            return new Pair<string, string>[][] { fs, ss };
        }

        /// <summary>
        /// 递归查询商品分类信息
        /// </summary>
        /// <param name="typid"></param>
        /// <returns></returns>
        public GoodType RecurType(int typid)
        {
            if (typid < 0) return null;
            GoodType gt = new GoodType();
            Goods_type t = DbLucky.Goods_type.First(r => r.Typeid == typid);
            gt.TypeID = t.Typeid;
            gt.TypeName = t.Typename;
            gt.ParentType = RecurType(t.Parentid);
            return gt;
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public bool Del(string guid)
        {
            if (guid == null) return false;
            var g = table.FirstOrDefault(f => f.Guid == guid);
            base.Del(g);
            return true;
        }
    }
}
